package org.jeecg.modules.demo.logics.service.impl;

import org.jeecg.modules.demo.logics.entity.OrderList;
import org.jeecg.modules.demo.logics.entity.CesConsumer;
import org.jeecg.modules.demo.logics.entity.OrderListGoods;
import org.jeecg.modules.demo.logics.mapper.CesConsumerMapper;
import org.jeecg.modules.demo.logics.mapper.OrderListGoodsMapper;
import org.jeecg.modules.demo.logics.mapper.OrderListMapper;
import org.jeecg.modules.demo.logics.service.IOrderListService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 订单表
 * @Author: jeecg-boot
 * @Date:   2021-12-17
 * @Version: V1.0
 */
@Service
public class OrderListServiceImpl extends ServiceImpl<OrderListMapper, OrderList> implements IOrderListService {

	@Autowired
	private OrderListMapper orderListMapper;
	@Autowired
	private CesConsumerMapper cesConsumerMapper;
	@Autowired
	private OrderListGoodsMapper orderListGoodsMapper;
	
	@Override
	@Transactional
	public void saveMain(OrderList orderList, List<CesConsumer> cesConsumerList,List<OrderListGoods> orderListGoodsList) {
		orderListMapper.insert(orderList);
		if(cesConsumerList!=null && cesConsumerList.size()>0) {
			for(CesConsumer entity:cesConsumerList) {
				//外键设置
				entity.setOrderListId(orderList.getId());
				cesConsumerMapper.insert(entity);
			}
		}
		if(orderListGoodsList!=null && orderListGoodsList.size()>0) {
			for(OrderListGoods entity:orderListGoodsList) {
				//外键设置
				entity.setOrderMainId(orderList.getId());
				orderListGoodsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(OrderList orderList,List<CesConsumer> cesConsumerList,List<OrderListGoods> orderListGoodsList) {
		orderListMapper.updateById(orderList);
		
		//1.先删除子表数据
		cesConsumerMapper.deleteByMainId(orderList.getId());
		orderListGoodsMapper.deleteByMainId(orderList.getId());
		
		//2.子表数据重新插入
		if(cesConsumerList!=null && cesConsumerList.size()>0) {
			for(CesConsumer entity:cesConsumerList) {
				//外键设置
				entity.setOrderListId(orderList.getId());
				cesConsumerMapper.insert(entity);
			}
		}
		if(orderListGoodsList!=null && orderListGoodsList.size()>0) {
			for(OrderListGoods entity:orderListGoodsList) {
				//外键设置
				entity.setOrderMainId(orderList.getId());
				orderListGoodsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		cesConsumerMapper.deleteByMainId(id);
		orderListGoodsMapper.deleteByMainId(id);
		orderListMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cesConsumerMapper.deleteByMainId(id.toString());
			orderListGoodsMapper.deleteByMainId(id.toString());
			orderListMapper.deleteById(id);
		}
	}
	
}
